/**
 * @author ken
 */
function JsonRpcProxy()
{
	
}

/**
 * Creates a proxy Method for the JsonRpc Service at the given url
 * @param {String} url
 */
JsonRpcProxy.prototype.createProxy = function(url, callback)
{
	var func = function(data, status)
	{
		
		var string = "(" +data+ ")" ;
		var result = eval(string);
		var proxy = new Object();
		proxy.targetUrl = url;
		/** @type {Array} */
		var services = result.services;
		for (var i=0; i < services.length; i++)
		{
			var name = services[i].name;
			/**  @type {Array} */
			var parameters = services[i].parameters;
			var names = [];
			var args  = [];
			for (var j=0; j < parameters.length; j++)
			{
				args.push(parameters[j].name);
				names.push(parameters[j].name);
			}
			names.push("__callback__");
			eval("proxy."+name+" = function("+names.join(",")+"){ \n"+
			"var request = new Object();\n"+
			"request.jsonrpc = '2.0';\n" +
			"request.method = '"+name + "';\n" +
			"request.id = 23;\n"+
			"request.params = ["+args.join(",")+"];\n" +
			"var jsonString = JSON.stringify(request);\n"+
			"$.ajax({type:'POST',url:'"+proxy.targetUrl+"', data:'json='+jsonString,success: function(response){\n" +
			"response = eval('('+response+')');"+
			"if (response.error)\n"+
			"{if (__callback__.error){__callback__.error(response.error);}}\n"+
			"else{__callback__.success(response.result);}\n"+
			"}" +
			", error:__callback__.serverError});"+
			"}");
		}
		callback(proxy);
	}
	$.get(url, func);
}


